package com.xh.core.util;

import com.aspose.words.Font;
import com.aspose.words.*;
import com.xh.tools.question.constant.QuestionConstant;

import java.awt.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class HandleNodeChangingFontChangerMaterial implements INodeChangingCallback {
    private double conSize;

    public HandleNodeChangingFontChangerMaterial(double conSize) {
        this.conSize = conSize;
    }

    @Override
    public void nodeInserting(NodeChangingArgs nodeChangingArgs) throws Exception {

    }

    @Override
    public void nodeInserted(NodeChangingArgs args) throws Exception {
        if (args.getNode().getNodeType() == NodeType.RUN) {
            Run run = (Run) args.getNode();
            Font font = run.getFont();
            if (RegexUtils.Match(run.getText(), "﹣")) {
                font.setName("宋体");
            }
//            else {
//                font.setName("宋体");
//            }
            // 处理试卷大标题的字号
            if (run.getText().startsWith("@@") && run.getText().endsWith("@@")) {
                //讲义大标题1
                run.setText(run.getText().substring(2, run.getText().length() - 2));
                font.setSize(16);
                font.setBold(true);
            } else {
                String text = HtmlUtils.recoverHtmlChar(run.getText());
                if (text.startsWith("$$") && text.endsWith("$$")){
                    //讲义设置小标题
                    run.setText(run.getText().replace("$$",""));
                    font.setSize(14);
                    font.setName("微软雅黑");
                    font.setBold(true);
                    Color color = new Color(0,122,192);
                    font.setColor(color);
                } else if (RegexUtils.Match(HtmlUtils.recoverHtmlChar(run.getText()), QuestionConstant.REGEX_MATCH_RULE.QUESTION_TYPE_START)) {
                    font.setSize(12);
                }
                else {
                    font.setSize(conSize);
                }
            }
        }
    }

    @Override
    public void nodeRemoving(NodeChangingArgs nodeChangingArgs) throws Exception {

    }

    @Override
    public void nodeRemoved(NodeChangingArgs nodeChangingArgs) throws Exception {

    }

}
